<template>
  <div class="my-comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <div style="display:flex; justify-content:space-between;">
          <div class="left-box">
            <van-image :src="item.aut_photo" round fit="cover" />
            <div class="info-box">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ $relvTime(item.pubdate) }}
                <van-button size="small"
                  @click="clickReply(item)"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
          </div>
          <van-icon name="like-o" />
        </div>
      </van-cell>
    </van-list>

    <!-- 回复面板 -->
    <van-action-sheet v-model="replyShow" title="回复列表">
      <reply-comment v-if="replyShow" :item="currentItem" @add="currentItem.reply_count++"/>
    </van-action-sheet>

    <div class="cmt-box">
      <van-field
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="send"
      >
        <template #button>
          <van-button size="mini" type="info" @click="send">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
// 导入接口
import { cmtListAPI, sendAPI } from '@/api/comment'
// 导入回复组件
import ReplyComment from './ReplyComment.vue'
export default {
  components: {
    ReplyComment
  },
  data () {
    return {
      replyShow: false,
      cmt: '',
      list: [],
      loading: false,
      finished: false,
      offset: null,
      currentItem: {}
    }
  },
  methods: {
    async onLoad () {
      const res = await cmtListAPI({
        // 获取评论就传a
        type: 'a',
        // 获取评论就传文章id，代表获取哪篇文章的评论
        source: this.$route.params.id,
        // null就是相当于没传，没传就是从第一页开始查
        offset: this.offset,
        limit: 10
      })

      // 本次的最后一条id
      this.offset = res.data.last_id

      // 把数据加到数组里
      this.list.push(...res.data.results)
      // 加载完本次记得把loading改为false
      this.loading = false

      // 判断是否查完了
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    },

    // 发布
    async send () {
      // 1. 非空判断
      if (this.cmt === '') return this.$toast('请输入内容')

      // 2. 有内容就要发请求给服务器
      const res = await sendAPI({
        target: this.$route.params.id,
        content: this.cmt
      })

      // 3. 输入内容清空
      this.cmt = ''

      // 4. 把新内容渲染出来
      this.list.unshift(res.data.new_obj)
    },

    // 点击回复
    clickReply (item) {
      // 1. 把sheet显示出来
      this.replyShow = true
      // 2. 点谁传谁
      this.currentItem = item
    }
  }
}
</script>

<style lang="less" scoped>
.my-comment {
  padding-bottom: 52px;
  .cmt-box {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }

  ::v-deep .left-box {
    display: flex;
    .van-image {
      margin-right: 10px;
      width: 40px;
      height: 40px;
    }
    .name {
      color: #5073a2;
      font-size: 12px;
    }
    .content {
      margin: 15px 0;
    }

    .time {
      color: gray;
      margin-right: 10px;
      font-size: 12px;
    }
    .van-button {
      font-size: 12px;
      vertical-align: middle;
    }
  }
}
</style>
